package cn.edu.heuet.bnve.activity.SettingActivity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;

import com.xuexiang.xui.widget.dialog.bottomsheet.BottomSheet;
import com.xuexiang.xui.widget.grouplist.XUICommonListItemView;
import com.xuexiang.xui.widget.grouplist.XUIGroupListView;

import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.activity.ScheduleActivity.SetAlarmToneActivity;
import cn.edu.heuet.bnve.databinding.ActivityNotifySettingBinding;
import cn.edu.heuet.bnve.utils.SharepreferencesUtilSystemSettings;

public class NotifySettingActivity extends AppCompatActivity {

    private ActivityNotifySettingBinding binding;
    private XUIGroupListView mGroupListView;
    private Toolbar toolbar;
    XUICommonListItemView item5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_notify_setting);
        initData();
        initView();
        initEvent();
    }

    public static void show(Context context) {
        context.startActivity(new Intent(context, NotifySettingActivity.class));
    }


    public void initData(){

    }

    public void initView(){
        mGroupListView = binding.notifyGroupListView;
        initGroupListView();

        toolbar=binding.toolbarContainer.toolbar;
        setSupportActionBar(toolbar);
        toolbar.setTitle("设置");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void initEvent(){


    }
    private void initGroupListView() {
        XUICommonListItemView item1 = mGroupListView.createItemView("开启通知");
        item1.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        item1.setTag(1);
        boolean item1Value = SharepreferencesUtilSystemSettings.getValue(this,"schedule_notify1",false);
        item1.getSwitch().setChecked(item1Value);
        item1.getSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    SharepreferencesUtilSystemSettings.putValue(NotifySettingActivity.this,"schedule_notify1",true);
                }
                else{
                    SharepreferencesUtilSystemSettings.putValue(NotifySettingActivity.this,"schedule_notify1",false);
                }
            }
        });

        XUICommonListItemView item2 = mGroupListView.createItemView(
                "通知显示样式");
        item2.setTag(2);
        item2.setOrientation(XUICommonListItemView.HORIZONTAL);
        item2.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        String[] item2Values ={"顶部显示","弹窗显示"};
        switch (SharepreferencesUtilSystemSettings.getValue(this,"schedule_notify_model",0)){
            case 0:
                item2.setDetailText(item2Values[0]);
                break;
            case 1:
                item2.setDetailText(item2Values[1]);
                break;
        }

        XUICommonListItemView item3 = mGroupListView.createItemView("开启震动");
        item3.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        item3.setOrientation(XUICommonListItemView.HORIZONTAL);
        item3.setTag(3);
        boolean item3Value = SharepreferencesUtilSystemSettings.getValue(this,"schedule_notify_isVibrate1",false);
        item3.getSwitch().setChecked(item3Value);
        item3.getSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    SharepreferencesUtilSystemSettings.putValue(NotifySettingActivity.this,"schedule_notify_isVibrate1",true);
                }
                else{
                    SharepreferencesUtilSystemSettings.putValue(NotifySettingActivity.this,"schedule_notify_isVibrate1",false);
                }
            }
        });

        XUICommonListItemView item4 = mGroupListView.createItemView("开启声音");
        item4.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        item4.setOrientation(XUICommonListItemView.HORIZONTAL);
        item4.setTag(4);
        boolean item4Value = SharepreferencesUtilSystemSettings.getValue(this,"schedule_notify_isSound1",false);
        item4.getSwitch().setChecked(item4Value);
        item4.getSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    SharepreferencesUtilSystemSettings.putValue(NotifySettingActivity.this,"schedule_notify_isSound1",true);
                }
                else{
                    SharepreferencesUtilSystemSettings.putValue(NotifySettingActivity.this,"schedule_notify_isSound1",false);
                }
            }
        });


        item5 = mGroupListView.createItemView(
                "选择通知铃声");
        item5.setTag(5);
        item5.setOrientation(XUICommonListItemView.HORIZONTAL);
        item5.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        item5.setDetailText(SharepreferencesUtilSystemSettings.getValue(this,"schedule_notify_sound","无"));



        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v instanceof XUICommonListItemView) {
                    int tag;
                    try {
                        tag = (Integer) ((XUICommonListItemView) v).getTag();
                        switch (tag){
                            case 2:
                                new BottomSheet.BottomListSheetBuilder(NotifySettingActivity.this)
                                        .setTitle("选择通知样式")
                                        .addItem("顶部显示")
                                        .addItem("弹窗显示")
                                        .setIsCenter(true)
                                        .setOnSheetItemClickListener((dialog, itemView, position, tag1) -> {
                                            dialog.dismiss();
                                            SharepreferencesUtilSystemSettings.putValue(NotifySettingActivity.this,"schedule_notify_model",position);
                                            item2.setDetailText(item2Values[position]);
                                        })
                                        .build()
                                        .show();
                                break;
                            case 5:
                                startActivityForResult(new Intent(NotifySettingActivity.this, SetAlarmToneActivity.class), 2);
                                break;
                        }
                    }
                    catch (Exception e){
                        tag =0;
                    }
                    if (tag!=0) {

                    }
                }
            }
        };

        XUIGroupListView.newSection(this)
                .setTitle("日程通知")
                .addItemView(item1, onClickListener)
                .addItemView(item2, onClickListener)
                .addTo(mGroupListView);

        XUIGroupListView.newSection(this)
                .setTitle("通知定制")
                .addItemView(item3, onClickListener)
                .addItemView(item4, onClickListener)
                .addItemView(item5, onClickListener)
                .addTo(mGroupListView);




    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
        if (requestCode == 2) {
            if (resultCode == 2) {
                if (data != null) {
                    String sound = data.getStringExtra("sound");
                    String soundPath = data.getStringExtra("soundPath");
                    item5.setDetailText(sound);
                    SharepreferencesUtilSystemSettings.putValue(this,"schedule_notify_sound",sound);
                    SharepreferencesUtilSystemSettings.putValue(this,"schedule_notify_soundPath",soundPath);

                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
        super.onActivityResult(requestCode, resultCode, data);

    }
}